package org.jeecg.modules.demo.com.kunpeng.controller;

import java.io.File;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.UUID;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hpsf.Decimal;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.com.kunpeng.entity.OaApprove;
import org.jeecg.modules.demo.com.kunpeng.entity.ProjectCostApply;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.ProjectCostApplyQueryDTO;
import org.jeecg.modules.demo.com.kunpeng.mapper.ProjectMapper;
import org.jeecg.modules.demo.com.kunpeng.service.IOaApproveService;
import org.jeecg.modules.demo.com.kunpeng.service.IProjectCostApplyService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.com.kunpeng.vo.FileVO;
import org.jeecg.modules.demo.com.kunpeng.vo.ProjectCostApplyListQueryVO;
import org.jeecg.modules.demo.com.kunpeng.vo.ProjectStandardVO;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 项目费用申请表
 * @Author: jeecg-boot
 * @Date:   2022-12-15
 * @Version: V1.0
 */
@Api(tags="项目费用申请表")
@RestController
@RequestMapping("/com.kunpeng/projectCostApply")
@Slf4j
public class ProjectCostApplyController extends JeecgController<ProjectCostApply, IProjectCostApplyService> {
	@Autowired
	private IProjectCostApplyService projectCostApplyService;
	@Autowired
	private ProjectMapper projectMapper;
	@Autowired
	private IOaApproveService oaApproveService;
	/**
	 * 分页列表查询
	 *
	 * @param projectCostApply
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "项目费用申请表-分页列表查询")
	@ApiOperation(value="项目费用申请表-分页列表查询", notes="项目费用申请表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ProjectCostApply>> queryPageList(ProjectCostApply projectCostApply,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ProjectCostApply> queryWrapper = QueryGenerator.initQueryWrapper(projectCostApply, req.getParameterMap());
		Page<ProjectCostApply> page = new Page<ProjectCostApply>(pageNo, pageSize);
		IPage<ProjectCostApply> pageList = projectCostApplyService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param projectCostApply
	 * @return
	 */
	@AutoLog(value = "项目费用申请表-添加")
	@ApiOperation(value="项目费用申请表-添加", notes="项目费用申请表-添加")
	//@RequiresPermissions("com.kunpeng:project_cost_apply:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ProjectCostApply projectCostApply) {
		/*LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();*/
		log.info("费用申请表添加数据："+projectCostApply.toString());
		String projectName = projectCostApply.getProjectName();
		/*projectCostApply.setApplyPerson(sysUser.getUsername());*/
		ProjectStandardVO projectStandardVO = projectMapper.selectByProjectName(projectName);
		projectCostApply.setProjectId(projectStandardVO.getProjectId());
		projectCostApply.setState("1");
		projectCostApplyService.save(projectCostApply);
		/*OaApprove oaApprove=new OaApprove();
		int num = oaApproveService.selectDateCount();
		oaApprove.setId(num++);
		oaApprove.setProjectPhase(projectCostApply.getProjectPhase());
		oaApprove.setAmount(projectCostApply.getCostMoney());
		oaApprove.setProjectName(projectCostApply.getProjectName());
		oaApprove.setType(27);
		oaApprove.setFlowId(27);
		oaApproveService.insert(oaApprove);*/
		return Result.OK("添加成功！");
	}
	
	/**
	  *  编辑
	  *
	  * @param projectCostApply
	  * @return
	  */
	 @AutoLog(value = "项目费用申请表-编辑")
	 @ApiOperation(value="项目费用申请表-编辑", notes="项目费用申请表-编辑")
	 //@RequiresPermissions("com.kunpeng:project_cost_apply:edit")
	 @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> edit(@RequestBody ProjectCostApply projectCostApply) {
		 projectCostApplyService.updateById(projectCostApply);
		 return Result.OK("编辑成功!");
	 }

	 /**
	  *  修改项目费用申请状态
	  *
	  * @param projectCostApply
	  * @return
	  */
	 @AutoLog(value = "项目费用申请表-修改项目费用申请状态")
	 @ApiOperation(value="项目费用申请表-修改项目费用申请状态", notes="项目费用申请表-修改项目费用申请状态")
	 @PostMapping(value = "/updateState")
	 public Result<String> updateState(@RequestBody ProjectCostApply projectCostApply) {
		 projectCostApplyService.updateById(projectCostApply);
		 return Result.OK("修改成功!");
	 }
	
	/**
	 *  通过id删除
	 *
	 * @param id
	 * @return
	 */
	/*@AutoLog(value = "项目费用申请表-通过id删除")
	@ApiOperation(value="项目费用申请表-通过id删除", notes="项目费用申请表-通过id删除")
	//@RequiresPermissions("com.kunpeng:project_cost_apply:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		projectCostApplyService.removeById(id);
		return Result.OK("删除成功!");
	}*/
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	/*@AutoLog(value = "项目费用申请表-批量删除")
	@ApiOperation(value="项目费用申请表-批量删除", notes="项目费用申请表-批量删除")
	//@RequiresPermissions("com.kunpeng:project_cost_apply:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.projectCostApplyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}*/
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "项目费用申请表-通过id查询")
	@ApiOperation(value="项目费用申请表-通过id查询", notes="项目费用申请表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ProjectCostApply> queryById(@RequestParam(name="id",required=true) String id) {
		ProjectCostApply projectCostApply = projectCostApplyService.getById(id);
		if(projectCostApply==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(projectCostApply);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param projectCostApply
    */
    //@RequiresPermissions("com.kunpeng:project_cost_apply:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ProjectCostApply projectCostApply) {
        return super.exportXls(request, projectCostApply, ProjectCostApply.class, "项目费用申请表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("com.kunpeng:project_cost_apply:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ProjectCostApply.class);
    }

	 /**
	  * 根据关键字模糊查询
	  *
	  * @param projectCostApplyQueryDTO
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @ApiOperation(value = "项目费用申请表-根据关键字分页列表查询", notes = "项目费用申请表-根据关键字分页列表查询")
	 @PostMapping(value = "/listByKeyWords")
	 public Result<IPage<ProjectCostApplyListQueryVO>> queryPageList(@RequestBody ProjectCostApplyQueryDTO projectCostApplyQueryDTO,
																	 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
																	 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		 log.info("开始处理根据关键字查询项目列表请求...");
		 Result<Page<ProjectCostApplyListQueryVO>> result = new Result<Page<ProjectCostApplyListQueryVO>>();
		 Page<ProjectCostApplyListQueryVO> pageList = new Page<ProjectCostApplyListQueryVO>(pageNo, pageSize);
		 pageList = projectCostApplyService.listByKeyWords(pageList, projectCostApplyQueryDTO);//通知公告消息

		 if (pageList.getRecords().size() == 0) {
			 return Result.error("没有符合条件的数据，请重新输入！");
		 }
		 log.info("查询当前页：" + pageList.getCurrent());
		 log.info("查询当前页数量：" + pageList.getSize());
		 log.info("查询结果数量：" + pageList.getRecords().size());
		 log.info("数据总数：" + pageList.getTotal());
		 result.setSuccess(true);
		 result.setResult(pageList);
		 return Result.ok(pageList);
	 }


	 /**
	  * 处理文件上传
	  *
	  * @param pic
	  * @return
	  */
	 @ApiOperation(value = "文件上传接口")
	 @PostMapping(value = "/upload")
	 public Result<FileVO> upload(@RequestPart("file") MultipartFile pic) {
		 String fileDir = "E:/projectCostApplyFiles";
		 String fileName = pic.getOriginalFilename();
		 //获取文件后缀名
		 String suffix = fileName.substring(fileName.lastIndexOf("."));
		 fileName = UUID.randomUUID() + suffix;
		 //文件夹不存在则创建
		 File dirFile = new File(fileDir);
		 if (!dirFile.exists()) {
			 dirFile.mkdirs();
		 }
		 //文件写入磁盘
		 String filePath = fileDir + "/" + fileName;
		 try {
			 pic.transferTo(new File(filePath));
		 } catch (IOException e) {
			 e.getMessage();
		 }
		 //返回文件对象
		 FileVO file = new FileVO();
		 file.setFileName(fileName);
		 file.setFileUrl(fileDir);
		 return Result.ok(file);
	 }

	/**
	 * 项目列表页面跳转
	 *
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value = "项目费用申请表-项目列表页面跳转", notes = "项目费用申请表-项目列表页面跳转")
	@GetMapping(value = "/listByPjId")
	public Result<IPage<ProjectCostApplyListQueryVO>> listByPjId(String ProjectPjId,
																	@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
																	@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		log.info("开始处理根据关键字查询项目列表请求...");
		Result<Page<ProjectCostApplyListQueryVO>> result = new Result<Page<ProjectCostApplyListQueryVO>>();
		Page<ProjectCostApplyListQueryVO> pageList = new Page<ProjectCostApplyListQueryVO>(pageNo, pageSize);
		pageList = projectCostApplyService.listByProjectId(pageList, ProjectPjId);//通知公告消息
		if (pageList.getRecords().size() == 0) {
			return Result.error("没有符合条件的数据，请重新输入！");
		}
		log.info("查询当前页：" + pageList.getCurrent());
		log.info("查询当前页数量：" + pageList.getSize());
		log.info("查询结果数量：" + pageList.getRecords().size());
		log.info("数据总数：" + pageList.getTotal());
		result.setSuccess(true);
		result.setResult(pageList);
		return Result.ok(pageList);
	}

	/**
	 *  根据id修改项目费用申请状态
	 *
	 * @return
	 */
	@AutoLog(value = "项目费用申请表-根据id修改项目费用申请状态")
	@ApiOperation(value="项目费用申请表-根据id修改项目费用申请状态", notes="项目费用申请表-根据id修改项目费用申请状态")
	@PostMapping(value = "/updateProjectCostApplyState")
	public Result<String> updateProjectCostApplyState(String id,String state) {
		projectCostApplyService.updateProjectCostApplyStatus(id,state);
		return Result.OK("修改成功!");
	}

}
